<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/22
 * Time: 11:11
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\common\model\BuddhaArticle;
use app\common\model\BuddhaArticleClick;
use app\common\model\BuddhaOrder;
use app\api\model\WeixinUser;
use app\common\lib\Show;
use app\common\lib\Status;

class User extends Base
{
    /**
     * 更新用户信息
     * @return array|\think\response\Json
     */
    public function updateByUserInfo()
    {
        $nickName = $this->request->param('nickName', '', 'trim');
        $avatarUrl = $this->request->param('avatarUrl', '', 'trim');
        $openid = $this->m_openid;
        if (empty($openid)) {
            return Show::json_show(Status::mini_error, "用户错误");
        }
        try {
            $result = (new WeixinUser())->updateByUserInfo([
                "nickName" => $nickName,
                "avatarUrl" => $avatarUrl,
                "openid" => $openid
            ]);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $result);
    }

    /**
     * 获取捐赠金额
     * @return array|\think\response\Json
     */
    public function getUserByList()
    {
        $openid = $this->request->param('openid', '', 'trim');
        if (empty($openid)) {
            return Show::json_show(Status::mini_error, "用户错误");
        }
        try {
            $money = (new BuddhaOrder())
                ->field('sum(money) as money')
                ->where('openid', $openid)
                ->where('payment', 1)
                ->find();
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "内部错误");
        }
        return Show::json_show(Status::mini_normal, "ok", [
            "money" => $money['money'] ?? 0.00
        ]);
    }

    //获取我的查看
    public function getArticleByList()
    {
        $openid = $this->request->param('openid', '', 'trim');
        if (empty($openid)) {
            return Show::json_show(Status::mini_error, "用户错误");
        }
        try {
            $lists = (new BuddhaArticleClick())
                ->field('a.article_id,a.create_time,p.title,p.short_title,p.logo,p.tag,p.id as id')
                ->alias('a')
                ->join('buddha_article p', 'a.article_id = p.id')
                ->where('openid', $openid)
                ->paginate(10)->toArray();
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "发生错误");
        }
        $row = [];
        if (!empty($lists['data'])) {
            foreach ($lists['data'] as $k => $v) {
                $row[$k] = $v;
                $tag = (new BuddhaArticle())->getBaseById($v['tag']);
                $row[$k]['tag'] = $tag ?? [];
            }
            $lists['data'] = $row;
        }
        return Show::json_show(Status::mini_normal, "ok", $lists);
    }

    /**
     * @return array|\think\response\Json
     */
    public function updateUserAddress()
    {
        $openid = $this->m_openid;
        $param = $this->request->param();
        try {
            $result = (new WeixinUser())->updateByUserInfo([
                "openid" => $openid,
                "adr_name" => $param['name'],
                "adr_lat" => $param['latitude'],
                "adr_lng" => $param['longitude'],
                "adr_address" => $param['address'],
            ]);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $result);
    }
}